<template>
    <el-dialog v-model="editData.visible" :title="editData.title" center width="30%" top="1vh" :close-on-click-modal="false" append-to-body>
        <div class="dialog-content">
            <el-form :model="editData.form" label-width="auto" style="width:100%" ref="formRef" :rules="editData.rules">
                <el-row :gutter="50">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="回复内容" prop="content">
                            <el-input v-model="editData.form.content" placeholder="请输入回复内容" type="textarea" :rows="8" clearable/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="editData.visible = false">取消</el-button>
                <el-button type="primary" @click="submitForm">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>


<script setup>
import { nextTick, reactive, ref, useTemplateRef } from 'vue'
import { ElMessage } from 'element-plus'
import replyApi from 'api/model/reply.js'

const emits = defineEmits(['update'])
defineExpose({ openDialog })
const formRef = useTemplateRef('formRef')
const editData = reactive({
    type: 'add',
    visible: false,
    form: {
        id: '',
        content: '',
    },
    rules: {
        content: [
            { required: true, message: '请输入回复内容', trigger: 'blur' },
        ],
    }
})

/**
 * 打开弹框
 * @param type
 * @param item
 */
function openDialog (type, item) {
    editData.title = type === 'add' ? '新增回复' : '编辑回复'
    editData.visible = true
    editData.type = type
    editData.form.id = ''
    editData.form.suggestId = item.suggestId
    nextTick(() => {
        formRef.value.resetFields()
        if (type === 'update') {
            const { id, content } = item || {}
            editData.form.id = id
            editData.form.content = content
        }
    })
}

/**
 * 提交表单
 */
function submitForm () {
    formRef.value.validate(valid => {
        if (!valid) return
        editData.type === 'add' ? addItem() : updateItem()
    })
}

/**
 *  新增操作
 */
async function addItem () {
    const { code, data = [] } = await replyApi.add.post({
        content: editData.form.content,
        suggestId: editData.form.suggestId,
        adminId: 1,
    })
    if (code === 200) {
        ElMessage.success('新增成功')
        editData.visible = false
        emits('update')
    } else {
        ElMessage.error('新增失败')
    }
}

/**
 *  更新操作
 */
async function updateItem () {
    const { code, data = [] } = await replyApi.update.post({
        id: editData.form.id,
        content: editData.form.content,
        suggestId: editData.form.suggestId,
        adminId: 1,
    })
    if (code === 200) {
        ElMessage.success('编辑成功')
        editData.visible = false
        emits('update')
    } else {
        ElMessage.error('编辑失败')
    }
}
</script>
